<?php
class Imageupload_Model_Insertimage implements Imageupload_Model_Mainable{
    
    private $action_handler;
    
    public function main(array $variables = null) {
        $allowedExts = array("gif", "jpeg", "jpg", "png");
        //var_dump($variables['files']);
        //die();
        $temp = explode(".", $variables['files']['image_name']["name"]);
        $extension = end($temp);
        if (
                (
                ($variables['files']['image_name']["type"] == "image/gif")
                || ($variables['files']['image_name']["type"] == "image/jpeg")
                || ($variables['files']['image_name']["type"] == "image/jpg")
                || ($variables['files']['image_name']["type"] == "image/pjpeg")
                || ($variables['files']['image_name']["type"] == "image/x-png")
                || ($variables['files']['image_name']["type"] == "image/png"))
                && in_array($extension, $allowedExts))
            {
            if ($variables['files']['image_name']["error"] > 0)
            {
                echo "Return Code: " . $variables['files']['image_name']["error"] . "<br>";
                //die();
            }
            else
            {
                echo "Upload: " . $variables['files']['image_name']["name"] . "<br>";
                echo "Type: " . $variables['files']['image_name']["type"] . "<br>";
                echo "Size: " . ($variables['files']['image_name']["size"] / 1024) . " kB<br>";
                echo "Temp file: " . $variables['files']['image_name']["tmp_name"] . "<br>";
                
                if (file_exists(WEB_ROOT_FOLDER."upload/" . $variables['files']['image_name']["name"]))
                {
                    echo $variables['files']['image_name']["name"] . " already exists. ";
                    //die();
                }
                else
                {
                    move_uploaded_file($variables['files']['image_name']["tmp_name"],"upload/" . $variables['files']['image_name']["name"]);
                    echo "Stored in: " . "upload/" . $variables['files']['image_name']["name"];
                    //die();
                }
            }
        }
        else
        {
            echo "Invalid file";
            //die();
            //header("Location: ".SITE_ROOT."zarzadzaj-zdjeciami.html");
        }
        $variables['file_name'] = $variables['files']['image_name']["name"];
        $this->action_handler = new Imageupload_Data_Saveimage;
        $this->action_handler->main($variables);
        header("Location: ".SITE_ROOT."zarzadzaj-zdjeciami.html");
    }

}
